﻿
@{
    Layout = null;
}

<div>
    <div class="input-group">
        <div class="col-3 pr-0" style="display: none">
            <label id="SAMPLE-AddSpec-SpecId">@ViewData["SpecId"]</label>
            <label id="SAMPLE-AddSpec-Parent-ModalID">@ViewData["ModalID"]</label>
        </div>
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">代码*</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-AddSpec-Code" class="form-control font-rfs border-radius-noleft" placeholder="必填" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">名称*</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-AddSpec-Name" class="form-control font-rfs border-radius-noleft" placeholder="必填" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">一级分类*</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <select id="SAMPLE-AddSpec-Select-FirstGroup" class="form-control font-rfs border-radius-noleft" style="width:90%"></select>
            <div class="col-11 pl-0 input-group position-absolute float-left"><input id="SAMPLE-AddSpec-Input-FirstGroup" class="form-control font-rfs border-radius-noleft noRightBorder" style="width:100%" placeholder="可输入新的类型" /></div>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">二级分类</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <select id="SAMPLE-AddSpec-Select-SecondGroup" class="form-control font-rfs border-radius-noleft" style="width:90%"></select>
            <div class="col-11 pl-0 input-group position-absolute float-left"><input id="SAMPLE-AddSpec-Input-SecondGroup" class="form-control font-rfs border-radius-noleft noRightBorder" style="width:100%" placeholder="可输入新的类型" /></div>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">描述</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-AddSpec-Description" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">标准值</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-AddSpec-StandardValue" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">-3σ</span>
        </div>
        <div class="col-3 pl-0 pr-0">
            <input id="SAMPLE-AddSpec-LowA" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
        <div class="col-3 pr-0 pl-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">-2σ</span>
        </div>
        <div class="col-3 pl-0">
            <input id="SAMPLE-AddSpec-LowB" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">+3σ</span>
        </div>
        <div class="col-3 pl-0 pr-0">
            <input id="SAMPLE-AddSpec-HighA" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
        <div class="col-3 pr-0 pl-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">+2σ</span>
        </div>
        <div class="col-3 pl-0">
            <input id="SAMPLE-AddSpec-HighB" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">验收标准</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-AddSpec-AcceptanceCriteria" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div>
    <br />
    <div class="text-right">
        <button id="Supplier-AddSpec-confirm" class="btn btn-primary font-rfs">确定</button>
        <button id="Supplier-AddSpec-cancel" type="button" class="btn btn-info font-rfs">取消</button>
    </div>
</div>

<script>
    var AddSpec_specId = $("#SAMPLE-AddSpec-SpecId").html();
    /*初始化页面控件值，加载一级类别*/
    $(document).ready(function() { 
        LoadSpecFirstGroup();
        if (AddSpec_specId != '' && AddSpec_specId != null && AddSpec_specId != undefined) {
            // 如果是编辑状态，初始化页面控件值
            $.post("GetSpec", {
                Id: AddSpec_specId
            }, function (data) {
                $("#SAMPLE-AddSpec-Code").val(data.code);  /*这个地方属性首字母都是小写，否则数据绑定不上*/
                $("#SAMPLE-AddSpec-Name").val(data.name);
                $("#SAMPLE-AddSpec-Input-FirstGroup").val(data.firstGroup);
                $("#SAMPLE-AddSpec-Input-SecondGroup").val(data.secondGroup);
                $("#SAMPLE-AddSpec-Description").val(data.description);
                $("#SAMPLE-AddSpec-StandardValue").val(data.standardValue);
                $("#SAMPLE-AddSpec-LowA").val(data.lowA);
                $("#SAMPLE-AddSpec-LowB").val(data.lowB);
                $("#SAMPLE-AddSpec-HighA").val(data.highA);
                $("#SAMPLE-AddSpec-HighB").val(data.highB);
                $("#SAMPLE-AddSpec-AcceptanceCriteria").val(data.acceptanceCriteria);
            });
        }
    });

    /*加载一级类别*/
    function LoadSpecFirstGroup() {
        $.post("GetSpecFirstGroup", {
            //Type: type  // 传递的参数，对应控制器里的参数
        }, function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#SAMPLE-AddSpec-Select-FirstGroup").append(new Option(data[i].firstGroup));
            }
            $("#SAMPLE-AddSpec-Select-FirstGroup").prepend(new Option("", ""));
            $("#SAMPLE-AddSpec-Select-FirstGroup").val("");
        });
    }

    $("#SAMPLE-AddSpec-Select-FirstGroup").change(function () {
        $("#SAMPLE-AddSpec-Input-FirstGroup").val($("#SAMPLE-AddSpec-Select-FirstGroup").find("option:selected").text());
        $("#SAMPLE-AddSpec-Input-SecondGroup").val('');

        $.post("GetSpecSecondGroup", { /* 加载 二级分类 信息*/
            FirstGroup: $("#SAMPLE-AddSpec-Select-FirstGroup").find("option:selected").text()  // 传递的参数，对应控制器里的参数
        }, function (data) {
            $("#SAMPLE-AddSpec-Select-SecondGroup").empty();
            for (var i = 0; i < data.length; i++) {
                $("#SAMPLE-AddSpec-Select-SecondGroup").append(new Option(data[i].secondGroup));
            }
            $("#SAMPLE-AddSpec-Select-SecondGroup").prepend(new Option("", ""));
            $("#SAMPLE-AddSpec-Select-SecondGroup").val("");
        });
    });

    $("#SAMPLE-AddSpec-Select-SecondGroup").change(function () {
        $("#SAMPLE-AddSpec-Input-SecondGroup").val($("#SAMPLE-AddSpec-Select-SecondGroup").find("option:selected").text());
    });

    $("#Supplier-AddSpec-confirm").click(function () {
        if ($.trim($("#SAMPLE-AddSpec-Code").val()).length == 0) {
            alert("指标代码不能为空！");
            $("#SAMPLE-AddSpec-Code").addClass('error-style');
            return false;
        }
        if ($.trim($("#SAMPLE-AddSpec-Name").val()).length == 0) {
            alert("指标名称不能为空！");
            $("#SAMPLE-AddSpec-Name").addClass('error-style');
            return false;
        }
        if ($.trim($("#SAMPLE-AddSpec-Input-FirstGroup").val()).length == 0) {
            alert("指标一级类型不能为空！");
            $("#SAMPLE-AddSpec-Input-FirstGroup").addClass('error-style');
            return false;
        }
        var stand = $.trim($("#SAMPLE-AddSpec-StandardValue").val());
        if (isNaN(stand)) {
            if (stand.replace(/[\s]/g, '').length == 0) {
                alert("指标 标准值 的值只能是数字，不能是文本！");
                $("#SAMPLE-AddSpec-StandardValue").addClass('error-style');
                return false;
            }
        }
        var lowa = $.trim($("#SAMPLE-AddSpec-LowA").val());
        if (isNaN(lowa)) {
            if (lowa.replace(/[\s]/g, '').length == 0) {
                alert("指标 -3σ 的值只能是数字，不能是文本！");
                $("#SAMPLE-AddSpec-LowA").addClass('error-style');
                return false;
            }
        }
        var lowb = $.trim($("#SAMPLE-AddSpec-LowB").val());
        if (isNaN(lowb)) {
            if (lowb.replace(/[\s]/g, '').length == 0) {
                alert("指标 -2σ 的值只能是数字，不能是文本！");
                $("#SAMPLE-AddSpec-LowB").addClass('error-style');
                return false;
            }
        }
        var higha = $.trim($("#SAMPLE-AddSpec-HighA").val());
        if (isNaN(higha)) {
            if (higha.replace(/[\s]/g, '').length == 0) {
                alert("指标 +3σ 的值只能是数字，不能是文本！");
                $("#SAMPLE-AddSpec-HighA").addClass('error-style');
                return false;
            }
        }
        var highb = $.trim($("#SAMPLE-AddSpec-HighB").val());
        if (isNaN(highb)) {
            if (highb.replace(/[\s]/g, '').length == 0) {
                alert("指标 +2σ 的值只能是数字，不能是文本！");
                $("#SAMPLE-AddSpec-HighB").addClass('error-style');
                return false;
            }
        }
        if (parseFloat(lowa) >= parseFloat(lowb)) {
            alert("3σ 的值只能比 2σ 的值小！");
            $("#SAMPLE-AddSpec-LowA").addClass('error-style');
            return false;
        }
        if (parseFloat(highb) >= parseFloat(higha)) {
            alert("+3σ 的值只能比 +2σ 的值大！");
            $("#SAMPLE-AddSpec-HighA").addClass('error-style');
            return false;
        }

        var jsonStr = {};
        jsonStr["Code"] = $.trim($("#SAMPLE-AddSpec-Code").val());
        jsonStr["Name"] = $.trim($("#SAMPLE-AddSpec-Name").val());
        jsonStr["FirstGroup"] = $.trim($("#SAMPLE-AddSpec-Input-FirstGroup").val());
        jsonStr["SecondGroup"] = $.trim($("#SAMPLE-AddSpec-Input-SecondGroup").val());
        jsonStr["Description"] = $.trim($("#SAMPLE-AddSpec-Description").val());
        jsonStr["StandardValue"] = $.trim($("#SAMPLE-AddSpec-StandardValue").val());
        jsonStr["LowA"] = $.trim($("#SAMPLE-AddSpec-LowA").val());
        jsonStr["LowB"] = $("#SAMPLE-AddSpec-LowB").val();
        jsonStr["HighA"] = $("#SAMPLE-AddSpec-HighA").val();
        jsonStr["HighB"] = $("#SAMPLE-AddSpec-HighB").val();
        jsonStr["AcceptanceCriteria"] = $.trim($("#SAMPLE-AddSpec-AcceptanceCriteria").val());

        if (AddSpec_specId == '' || AddSpec_specId == null || AddSpec_specId == undefined) {
            $.post("postAddSampleSpec", {
                jsonStr: JSON.stringify(jsonStr)
            }, function(data) {
                alert(data);
                var modalID = $("#SAMPLE-AddSpec-Parent-ModalID").html(); // 这两行代码不能提到最后面，否则因为异步会造成刷新不到数据。
                $("#" + modalID).modal('hide');
            });
        } else {
            jsonStr["Id"] = AddSpec_specId;
            $.post("postUpdateSampleSpec", {
                jsonStr: JSON.stringify(jsonStr)
            }, function(data) {
                alert(data);
                var modalID = $("#SAMPLE-AddSpec-Parent-ModalID").html();
                $("#" + modalID).modal('hide');
            });
        }

    });

    $("#Supplier-AddSpec-cancel").click(function () {
        var modalID = $("#SAMPLE-AddSpec-Parent-ModalID").html();
        $("#" + modalID).modal('hide');
    });
    
</script>
